#include<iostream>
using namespace std;
#include<cmath>
#include<iomanip>
#include"library.h"

/*
 * Naive method to calculate the derivative of func at x,
 * using formula:
 * 	f'(x) = (f(x+h) - f(x-h))/2h + O(h^2)
 * h is the step, note that if it's too small, the round off error becomes large enought that the derivative makes no sense.
 */
double naive_deriv( double x, double h, double (*func)(double) ){

	if( fabs(h)<1E-9 ){	
		cout<<" Error in deriv_f(): h <= 1E-9 "<<endl;
		exit(1);
	}
	return ( func(x+h) - func(x-h) )/(2*h);
}

int main(){

	double x[6]={0,1,2,3,4,5};
	double h = 0.1;
	int n = 1;
	cout<<"n="; cin>>n;
	cout<<"D^0_"<<n<<" = "<<setprecision(15)<<Richardson_deriv(x[0], h, n, exp)<<endl;
	/*
	for(int i=0;i<6;i++){
		//cout<<" Derivative: naive="<<naive_deriv(x[i], h, exp);
		//cout<<"\t naive/error="<<naive_deriv(x[i], h, exp) - exp(x[i]);
		printf("%.8lf \t %.8lf \t %.8lf \t %.11lf\n",
		Richardson_deriv(x[i], h, 0, exp),
		Richardson_deriv(x[i], h, 1, exp),
		Richardson_deriv(x[i], h, 2, exp),
		- exp(x[i]) + Richardson_deriv(x[i], h, 2, exp) );
	}
	*/
	return 0;
}
